﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Identity.Models;

public class User: IdentityUser
{
    /// <summary>
    /// 获得/设置 用户主键ID
    /// </summary>
    [AutoGenerateColumn(Ignore =true,IsReadonlyWhenEdit =true,IsReadonlyWhenAdd =true)]
    public override string? Id { get; set; }=Guid.NewGuid().ToString();
    /// <summary>
    /// 获得/设置 系统登录用户名
    /// </summary>
    [Display(Name = "登录名称")]
    [Required(ErrorMessage = "{0}不可为空")]
    [AutoGenerateColumn(Ignore =false, Sortable = true, Filterable = true,IsReadonlyWhenEdit =true )]
    [RegularExpression("^[a-zA-Z0-9_@.]*$", ErrorMessage = "登录名称包含非法字符")]
    [MaxLength(16, ErrorMessage = "{0}不能超过 16 个字符")]
    [NotNull]
    public override string? UserName { get; set; }

    /// <summary>
    /// 获得/设置 用户显示名称
    /// </summary>
    [Display(Name = "显示名称")]
    [Required(ErrorMessage = "{0}不可为空")]
    [MaxLength(20, ErrorMessage = "{0}不能超过 20 个字符")]
    [NotNull]
    [AutoGenerateColumn(Ignore =false, Sortable = true, Filterable = true)]
    public string? DisplayName { get; set; }

    /// <summary>
    /// 获得/设置 用户头像图标路径
    /// </summary>
    [Display(Name = "用户头像")]
    [AutoGenerateColumn(Ignore =false, IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    
    public string? Icon { get; set; }

    /// <summary>
    /// 获得/设置 用户设置样式表名称
    /// </summary>
    [Display(Name = "主题")]
    [AutoGenerateColumn(Ignore =false, IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    public string? Css { get; set; }

    /// <summary>
    /// 获得/设置 用户默认登陆 App 标识
    /// </summary>
    [Display(Name = "默认 APP")]
    [NotNull]
     [AutoGenerateColumn(Ignore =false, IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    public string? App { get; set; } = "main";




    /// <summary>
    /// 获得/设置 用户注册时间
    /// </summary>
    [Display(Name = "注册时间")]
    [AutoGenerateColumn(Ignore =false, Sortable = true, Filterable = true,IsReadonlyWhenEdit =true,IsReadonlyWhenAdd =true)]
    public DateTime RegisterTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 获得/设置 用户被批复时间
    /// </summary>
    [Display(Name = "授权时间")]
    [AutoGenerateColumn(Ignore =false, IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    public DateTime? ApprovedTime { get; set; }

    /// <summary>
    /// 获得/设置 用户批复人
    /// </summary>
    [Display(Name = "授权人")]
    [AutoGenerateColumn(Ignore =false, IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    public string? ApprovedBy { get; set; }

    /// <summary>
    /// 获得/设置 用户的申请理由
    /// </summary>
    [Display(Name = "说明")]
    [NotNull]
    [Required]
    [AutoGenerateColumn(Ignore =false)]
    public string? Description { get; set; }

 


    [Display(Name = "企业微信ID")]
    [AutoGenerateColumn(Ignore =false)]

    public  string? WeChatId { get; set; } = string.Empty;
    [AutoGenerateColumn(Ignore =false)]
   
    public  通知方式 NotificationType { get; set; } = 通知方式.默认;
    /// <summary>
    /// 获得/设置 默认格式为 DisplayName (UserName)
    /// </summary>
    /// <returns></returns>
    public override string ToString() => $"{DisplayName} ({UserName})";

    /// <summary>
    /// 导航属性：通过 UserRole 访问角色
    /// </summary>
    [AutoGenerateColumn(Ignore =true)]
    public ICollection<UserRole> UserRoles { get; set; } = new List<UserRole>();
    [NotMapped] // 标记为不在数据库中映射
    [Display(Name = "角色")]
    [AutoGenerateColumn(Ignore =false)]
    public ICollection<Role> Roles => UserRoles.Select(ur => ur.Role).ToList();
 

  
   
    [AutoGenerateColumn(Ignore =false, Sortable = true, Filterable = true)]
    [Display(Name = "手机号码")]
    [Phone(ErrorMessage = "手机号码格式不正确")]
    public override string? PhoneNumber { get; set; }


    [AutoGenerateColumn(Ignore =false, Sortable = true, Filterable = true)]
    [Display(Name = "邮箱")]
    [EmailAddress(ErrorMessage = "邮箱格式不正确")]
    public override string? Email { get; set; }

    [AutoGenerateColumn(Ignore =true)]
    public override bool EmailConfirmed { get; set; } = true;
    [AutoGenerateColumn(Ignore =true)]
    public override bool PhoneNumberConfirmed { get; set; } = true;
    [AutoGenerateColumn(Ignore =true)]
    public override string? ConcurrencyStamp { get => base.ConcurrencyStamp; set => base.ConcurrencyStamp = value; }
    [AutoGenerateColumn(Text ="是否锁定", IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    public override bool LockoutEnabled { get => base.LockoutEnabled; set => base.LockoutEnabled = value; }
    [AutoGenerateColumn(Ignore =true)]
    public override int AccessFailedCount { get => base.AccessFailedCount; set => base.AccessFailedCount = value; }
    [AutoGenerateColumn(Text ="锁定时间", IsVisibleWhenAdd=false, IsVisibleWhenEdit =false)]
    public override DateTimeOffset? LockoutEnd { get => base.LockoutEnd; set => base.LockoutEnd = value; }
    [AutoGenerateColumn(Ignore =true)]
    public override string? NormalizedEmail { get => base.NormalizedEmail; set => base.NormalizedEmail = value; }
    [AutoGenerateColumn(Ignore =true)]
    public override string? NormalizedUserName { get => base.NormalizedUserName; set => base.NormalizedUserName = value; }
    [AutoGenerateColumn(Ignore =true)]
    public override string? PasswordHash { get => base.PasswordHash; set => base.PasswordHash = value; }
    [AutoGenerateColumn(Ignore =true)]
    public override string? SecurityStamp { get => base.SecurityStamp; set => base.SecurityStamp = value; }
    [AutoGenerateColumn(Ignore =true)]
    public override bool TwoFactorEnabled { get => base.TwoFactorEnabled; set => base.TwoFactorEnabled = value; }
    
  
    
}

 public  enum 通知方式
    {
        [Display(Name = "企业微信")]
        企业微信,
        [Display(Name = "邮件")]
        邮件,
        [Display(Name = "短信")]
        短信,
        [Display(Name = "系统通知")]
        默认,
    }